package cn.wolfcode.util;

import org.apache.commons.io.FileUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

public class FileOutUtil {
    public static String uploadFile(MultipartFile file,String realPath) throws IOException {
        //获取随机数
        String uuid = UUID.randomUUID().toString();
        //获取文件名称
        String name = file.getOriginalFilename();
        System.out.println(name);
        //获取文件后缀
        String suffix = name.substring(name.lastIndexOf("."));
        //文件名
        String fileName = "/upload/" + uuid + suffix;
        //获取目标路劲
        File targetFile = new File(realPath,fileName);
        //通过工具类把文件拷贝到目标路劲
        FileUtils.copyInputStreamToFile(file.getInputStream(),targetFile);
        return fileName;
    }

    public static void deleteFile(String fileName){
        File file = new File(fileName);
        if (file.exists()){
            file.delete();
        }
    }
}
